package com.hckjsec.cloud.filters;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.filter.RepeatedlyRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import com.hckjsec.common.utils.StringUtils;

/**
 * 过滤微信小程序端请求数据的重新封装
 */
@WebFilter(filterName = "wechatFilter",urlPatterns = {"/wechat/api/*"})
public class WechatFilter implements Filter {
//    private static final Logger log = LoggerFactory.getLogger(WechatFilter.class);
//    public static List<String> authorityList = new ArrayList<String>();
//    static {
//        if(authorityList.size()==0){
//            authorityList.add("/wechat/api/global");
//            authorityList.add("/wechat/api/checkUser");
//        }
//    }
//    /**
//     * 请求前缀 初始化
//     */
//    public static final String TOKEN_RELEASE = "Release ";
//    /**
//     * 请求前缀 访问资源
//     */
//    public static final String TOKEN_VERIFY = "Verify ";
    @Override
    public void doFilter(ServletRequest rquest, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        if (rquest instanceof HttpServletRequest && StringUtils.equalsAnyIgnoreCase(rquest.getContentType(),
                MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE))
        {
            requestWrapper = new RepeatedlyRequestWrapper((HttpServletRequest) rquest, response);
        }
        System.out.println("进入过滤器======="+((RepeatedlyRequestWrapper) requestWrapper).getRequestURI());
        if (null == requestWrapper)
        {
            chain.doFilter(rquest, response);
        }
        else
        {
            chain.doFilter(requestWrapper, response);
        }
//        HttpServletRequest req=(HttpServletRequest)rquest;
//        HttpServletResponse res=(HttpServletResponse) response;
//        HckjHttpServletRequestWrapper hcreq= new HckjHttpServletRequestWrapper(req);
//        Map<String, String []> parameterMap = new HashMap<String,  String []>(hcreq.getParameterMap());
//        String[] strs = new String[1];
//        boolean flag=false;
//        String headerToken = StringUtils.isHcNull(req.getParameter("token"),"");
//        if(!StringUtils.isEmpty(headerToken)){
//            if (authorityList.contains(hcreq.getRequestURI())){
//                headerToken=TOKEN_RELEASE+headerToken;
//            }else{
//                headerToken=TOKEN_VERIFY+headerToken;
//            }
//        }
//        if (StringUtils.isEmpty(headerToken)){//字符串为空  请求失败
//            returnJson(res, AjaxResult.cloud_601());
//            flag=false;
//        }else if (headerToken.startsWith(TOKEN_RELEASE)){
//            String appid=headerToken.replace(TOKEN_RELEASE, "");
//            if (StringUtils.isEmpty(appid)){
//                returnJson(res, AjaxResult.cloud_602("appId不能为空"));
//                flag=false;
//            }else{
//                strs[0] = appid;
//                parameterMap.put("appid",strs);
//                flag=true;
//            }
//        }else if (headerToken.startsWith(TOKEN_VERIFY)) {
//            String openid=headerToken.replace(TOKEN_VERIFY, "");
//            if (StringUtils.isEmpty(openid)){
//                returnJson(res, AjaxResult.cloud_602("openid不能为空"));
//                flag=false;
//            }else{
//                strs[0] = openid;
//                parameterMap.put("openid",strs);
//                flag=true;
//            }
//        }else{
//            returnJson(res, AjaxResult.cloud_601());
//            flag=false;
//        }
//        if (flag){
//            hcreq.setParameterMap(parameterMap);
//            chain.doFilter(hcreq,res);
//        }
    }


//    private void returnJson(HttpServletResponse response, AjaxResult ajaxResult){
//        PrintWriter writer = null;
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html; charset=utf-8");
//        try {
//            writer = response.getWriter();
//            writer.print(JSON.toJSONString(ajaxResult));
//        } catch (IOException e) {
//            log.error("微信端请求失败", e);
//        } finally {
//            if (writer != null)
//                writer.close();
//        }
//    }


}
